package org.jeecg.modules.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.jeecg.modules.hc.entity.HcCompanyDetail;
import org.jeecg.modules.hc.entity.HcCompanyInfo;
import org.jeecg.modules.hc.entity.HcCompanyMerchantInfo;
import org.jeecg.modules.hc.mapper.HcCompanyDetailMapper;
import org.jeecg.modules.hc.mapper.HcCompanyInfoMapper;
import org.jeecg.modules.hc.mapper.HcCompanyMerchantInfoMapper;
import org.jeecg.modules.hc.service.IHcCompanyMerchantInfoService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 企业商户关系表
 * @Author: jeecg-boot
 * @Date:   2023-07-04
 * @Version: V1.0
 */
@Service
@AllArgsConstructor
public class HcCompanyMerchantInfoServiceImpl extends ServiceImpl<HcCompanyMerchantInfoMapper, HcCompanyMerchantInfo> implements IHcCompanyMerchantInfoService {

    private HcCompanyMerchantInfoMapper hcCompanyMerchantInfoMapper;
    private HcCompanyInfoMapper hcCompanyInfoMapper;
    private HcCompanyDetailMapper hcCompanyDetailMapper;

    @Override
    public List<HcCompanyInfo> queryCompanyListByMerchantId(String merchantId) {
        List<HcCompanyInfo> hcCompanyInfos = new ArrayList<>();
        List<HcCompanyDetail> hcCompanyDetails  = hcCompanyDetailMapper.selectList(new LambdaQueryWrapper<HcCompanyDetail>());
        if (CollectionUtils.isNotEmpty(hcCompanyDetails)){
            List<String> companyIdList = new ArrayList<>();
            for (HcCompanyDetail hcCompanyDetail : hcCompanyDetails) {
                if(null != hcCompanyDetail && StringUtils.isNotEmpty(hcCompanyDetail.getListingStoreIds())){
                    if (hcCompanyDetail.getListingStoreIds().contains(merchantId)){
                        companyIdList.add(hcCompanyDetail.getCompanyId());
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(companyIdList)){
                List<HcCompanyInfo> hcCompanyInfoLists = hcCompanyInfoMapper.selectList(new LambdaQueryWrapper<HcCompanyInfo>().in(HcCompanyInfo::getId,companyIdList));
                hcCompanyInfos.addAll(hcCompanyInfoLists);
            }
        }
        return hcCompanyInfos;
    }

}
